home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 24 / Amiga Format AFCD24 (Feb 1998, Issue 108).iso / -in_the_mag- / emulation / amiga / uae-0.7.0b2 / src / od-win32 / joystick.c < prev    next >
C/C++ Source or Header  |  1998-01-20  |  2KB  |  111 lines

  1. /* 
  2.  * UAE - The Un*x Amiga Emulator
  3.  *
  4.  * Win32 interface
  5.  *
  6.  * Copyright 1997 Mathias Ortmann
  7.  */
  8.  
  9. #ifdef __GNUC__
  10. #define __int64 long long
  11. #include "machdep/winstuff.h"
  12. #else
  13. #include <windows.h>
  14. #include <ddraw.h>
  15. #include <stdlib.h>
  16. #include <stdarg.h>
  17. #include <commctrl.h>
  18. #include <commdlg.h>
  19. #include <stdio.h>
  20. #include <fcntl.h>
  21. #include <sys/stat.h>
  22. #include <io.h>
  23. #endif
  24.  
  25. #include "config.h"
  26. #include "sysconfig.h"
  27. #include "sysdeps.h"
  28. #include "options.h"
  29. #include "osdep/win32gui.h"
  30. #include "include/memory.h"
  31. #include "resource.h"
  32. #include "osdep/win32.h"
  33.  
  34. /* Joystick emulation, Win32 interface */
  35. #define MAXJOYSTICKS 2
  36.  
  37. static int joystickpresent[MAXJOYSTICKS];
  38. static int unplugged[MAXJOYSTICKS];
  39. static int warned[MAXJOYSTICKS];
  40. static JOYCAPS jc[MAXJOYSTICKS];
  41. static JOYINFOEX ji =
  42. {sizeof (ji), JOY_RETURNBUTTONS | JOY_RETURNX | JOY_RETURNY};
  43.  
  44. void read_joystick (int nr, short *dir, int *button)
  45. {
  46.     int left = 0, right = 0, top = 0, bot = 0;
  47.     int joyerr;
  48.  
  49.     *dir = *button = 0;
  50.     if (!joystickpresent[nr])
  51.     return;
  52.  
  53.     if (unplugged[nr]) {
  54.     unplugged[nr]--;
  55.     return;
  56.     }
  57.     switch (joyerr = joyGetPosEx (JOYSTICKID1 + nr, &ji)) {
  58.     case JOYERR_NOERROR:
  59.     warned[nr] = 0;
  60.     break;
  61.     case JOYERR_UNPLUGGED:
  62.     if (!warned[nr]) {
  63.         fprintf (stderr, "Joystick %d seems to be unplugged.\n", nr);
  64.         warned[nr] = 1;
  65.     }
  66.     unplugged[nr] = 200;
  67.     return;
  68.     default:
  69.     fprintf (stderr, "Error reading joystick %d: %d.\n", nr, joyerr);
  70.     unplugged[nr] = 100;
  71.     return;
  72.     }
  73.  
  74.     if (ji.dwXpos < (jc[nr].wXmin + 2 * (jc[nr].wXmax - jc[nr].wXmin) / 5))
  75.     left = 1;
  76.     else if (ji.dwXpos > (jc[nr].wXmin + 3 * (jc[nr].wXmax - jc[nr].wXmin) / 5))
  77.     right = 1;
  78.  
  79.     if (ji.dwYpos < (jc[nr].wYmin + 2 * (jc[nr].wYmax - jc[nr].wYmin) / 5))
  80.     top = 1;
  81.     else if (ji.dwYpos > (jc[nr].wYmin + 3 * (jc[nr].wYmax - jc[nr].wYmin) / 5))
  82.     bot = 1;
  83.  
  84.     if (left)
  85.     top = !top;
  86.     if (right)
  87.     bot = !bot;
  88.     *dir = bot | (right << 1) | (top << 8) | (left << 9);
  89.     *button = ji.dwButtons;
  90. }
  91.  
  92. void init_joystick (void)
  93. {
  94.     int nr;
  95.     int found = 0;
  96.  
  97.     for (nr = 0; nr < MAXJOYSTICKS; nr++) {
  98.     if (joyGetDevCaps (JOYSTICKID1 + nr, jc + nr, sizeof (jc)) == JOYERR_NOERROR) {
  99.         printf ("Joystick %d: %s with %d buttons\n", nr, jc[nr].szPname, jc[nr].wNumButtons);
  100.         joystickpresent[nr] = 1;
  101.         found++;
  102.     }
  103.     }
  104.  
  105.     fprintf (stdout, "%d joystick(s) found.\n", found);
  106. }
  107.  
  108. void close_joystick (void)
  109. {
  110. }
  111.